package in.muton.edushare.summary.html;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.text.SimpleDateFormat;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.ProfessionalDetailsDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.ProfessionalDetails;
import com.edushare.common.vo.Publications;
import com.edushare.common.vo.Users;

public class UserSummary extends WebPage {

	public UserSummary(IModel<Users> userModel) {
		init(userModel);
	}

	public UserSummary(PageParameters params) {
		int id = params.getAsInteger("0", 0);
		Users u = new UsersDAO(EduShareSession.get().getSessionORMInterface())
				.getUserByPk(id);
		init(new CayenneModel<Users>(u));
	}

	private void init(IModel<Users> userModel) {
		this.userModel = userModel;
		Users u = userModel.getObject();
		add(CSSPackageResource.getHeaderContribution(UserSummary.class, "UserPrinterFriendlyCSS.css"));
		add(new Label("title", u.getCompleteUserName()));
		add(new Label("ownerName", u.getCompleteUserName()));
		add(new Label("currentRole", ""));
		add(new Label("emailId", "Email : " + u.getEmail())
				.setVisible(StringUtil.isValid(u.getEmail())));
		add(new Label("phone", "Phone : " + u.getPhoneNo())
				.setVisible(StringUtil.isValid(u.getPhoneNo())));

		add(new MultiLineLabel("mySumm", u.getAboutMe()).setEscapeModelStrings(
				false).setVisible(StringUtil.isValid(u.getAboutMe())));

		CayenneList<ProfessionalDetails> acadList = new CayenneList<ProfessionalDetails>(
				new ProfessionalDetailsDAO(EduShareSession.get()
						.getSessionORMInterface()).getdegrees(u));

		WebMarkupContainer wmc = new WebMarkupContainer("academicContainer");
		wmc.setVisible(acadList.getObject().size() > 0);
		add(wmc);
		wmc.add(new ListView<IModel<ProfessionalDetails>>("acadsLV", acadList
				.getObject()) {
			private static final long serialVersionUID = 4491799373216638802L;

			@Override
			protected void populateItem(
					final ListItem<IModel<ProfessionalDetails>> arg0) {
				ProfessionalDetails pd = arg0.getModelObject().getObject();
				arg0.add(new Label("degree", pd.getPositionTitle()));
				arg0.add(new Label("spl", pd.getSpecialization()) {
					private static final long serialVersionUID = 5815245917015239585L;

					@Override
					public boolean isVisible() {
						return this.getDefaultModelObject() != null
								&& StringUtil
										.isValid(getDefaultModelObjectAsString());
					}
				});
				arg0.add(new Label("school", pd.getOrgName()));
				arg0.add(new Label("from", new Model<String>() {
					private static final long serialVersionUID = 1L;

					@Override
					public String getObject() {
						ProfessionalDetails pd = arg0.getModelObject()
								.getObject();
						if (pd.getFromDate() == null)
							return "";
						return new SimpleDateFormat("MMM, yyyy").format(pd
								.getFromDate());
					}

				}));
				arg0.add(new Label("to", new Model<String>() {
					private static final long serialVersionUID = 1L;

					@Override
					public String getObject() {
						ProfessionalDetails pd = arg0.getModelObject()
								.getObject();
						if (pd.getToDate() == null || pd.getIsCurrent()) {
							return "Present";
						}

						return new SimpleDateFormat("MMM, yyyy").format(pd
								.getToDate());
					}
				}));
				arg0.add(new Label("eduSumm", pd.getDescription())
						.setEscapeModelStrings(false));

			}
		});

		CayenneList<ProfessionalDetails> profList = new CayenneList<ProfessionalDetails>(
				new ProfessionalDetailsDAO(EduShareSession.get()
						.getSessionORMInterface()).getPositions(u));
		WebMarkupContainer wmc2 = new WebMarkupContainer("expContainer");
		wmc2.setVisible(profList.getObject().size() > 0);
		add(wmc2);
		wmc2.add(new ListView<IModel<ProfessionalDetails>>("expLV", profList
				.getObject()) {
			private static final long serialVersionUID = -412210789567086215L;

			@Override
			protected void populateItem(
					final ListItem<IModel<ProfessionalDetails>> arg0) {
				ProfessionalDetails pd = arg0.getModelObject().getObject();
				arg0.add(new Label("role", pd.getPositionTitle()));
				arg0.add(new Label("org", pd.getOrgName()));
				arg0.add(new Label("from", new Model<String>() {
					private static final long serialVersionUID = 1L;

					@Override
					public String getObject() {
						ProfessionalDetails pd = arg0.getModelObject()
								.getObject();
						if (pd.getFromDate() == null)
							return "";
						return new SimpleDateFormat("MMM, yyyy").format(pd
								.getFromDate());
					}

				}));
				arg0.add(new Label("to", new Model<String>() {
					private static final long serialVersionUID = 1L;

					@Override
					public String getObject() {
						ProfessionalDetails pd = arg0.getModelObject()
								.getObject();
						if (pd.getToDate() == null || pd.getIsCurrent()) {
							return "Present";
						}

						return new SimpleDateFormat("MMM, yyyy").format(pd
								.getToDate());
					}
				}));
				arg0.add(new Label("jobSumm", pd.getDescription())
						.setEscapeModelStrings(false));
			}
		});

		add(new MultiLineLabel("awards", u.getHonors()).setEscapeModelStrings(
				false).setVisible(StringUtil.isValid(u.getHonors())));
		/*add(new MultiLineLabel("publications", "").setEscapeModelStrings(false)
				.setVisible(false));*/
		CayenneList<Publications> pubList = new CayenneList<Publications>(u.getPublications());
		WebMarkupContainer wmc3 = new WebMarkupContainer("pubContainer");
		wmc3.setVisible(pubList.getObject().size() > 0);
		add(wmc3);
		wmc3.add(new ListView<IModel<Publications>>("pubLV", pubList.getObject()) {

			private static final long serialVersionUID = -4976553409124965976L;

			@Override
			protected void populateItem(final ListItem<IModel<Publications>> ps) {
				Publications p = ps.getModelObject().getObject();
				ps.add(new Label("pubTtl", p.getTitle()));
				ps.add(new Label("pubAuth", p.getAuthors()));
				ps.add(new Label("pubJourn", p.getJournal()));
				ExternalLink url = new ExternalLink("pubLink", p.getLinkToPaper(),p.getLinkToPaper());
				ps.add(url);
				if(!StringUtil.isValid(p.getLinkToPaper()))
					url.setVisible(false);
				ps.add(new Label("pubSumm", p.getAbstract()).setEscapeModelStrings(false));
			}
		});
		
	}

	private UserDataHandler udh;
	private IModel<Users> userModel;
}
